package cn.zhihong.controller;

import cn.zhihong.common.R;
import cn.zhihong.pojo.ShoppingCart;
import cn.zhihong.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;

/**
 * @ClassName ShoppingCartController
 * @Author 庄志宏
 * @Date 2022/4/27 15:39
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * @author 庄志宏
     * @date: 2022/4/27 15:40
     * 新增购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        //获取userId
        Long userId = (Long)session.getAttribute("user");
        shoppingCart.setUserId(userId);
        shoppingCart.setCreateTime(LocalDate.now());
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId != null){
            //添加到购物车的是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shopping = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (shopping != null){
            shopping.setNumber(shopping.getNumber()+1);
            shoppingCartService.updateById(shopping);
        }else {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            shopping = shoppingCart;
        }
        return R.success(shopping);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/27 18:59
     * 查询购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpSession session){
        log.info("查询购物车===");
        Long userId = (Long)session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/27 19:03
     * 减去购物车
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart,HttpSession session){
        log.info("减去购物车{}",shoppingCart);
        Long userId = (Long) session.getAttribute("user");
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        if (dishId != null){
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shopping = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        Integer number = shopping.getNumber();
        if (number>1){
            shopping.setNumber(number-1);
            shoppingCartService.updateById(shopping);
        }else {
            shopping.setNumber(0);
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        }
        return R.success(shopping);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/27 19:40
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
        return R.success("清空购物车成功");
    }
}
